package com.ls.lserp.modules.order.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ls.lserp.modules.sys.utils.SystemMessageUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.BaseEntity;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.FileUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.utils.SystemPath;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.order.entity.AuditInfo;
import com.ls.lserp.modules.order.entity.BigcargoOrder;
import com.ls.lserp.modules.order.entity.BigcargoStyle;
import com.ls.lserp.modules.order.entity.CustomizationContract;
import com.ls.lserp.modules.order.entity.Quotation;
import com.ls.lserp.modules.order.entity.SpecimenProduction;
import com.ls.lserp.modules.order.entity.UploadFileData;
import com.ls.lserp.modules.order.service.BigcargoOrderService;
import com.ls.lserp.modules.order.service.CustomizationContractService;
import com.ls.lserp.modules.order.service.QuotationService;
import com.ls.lserp.modules.sys.utils.UserUtils;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import groovy.util.ResourceException;

/**
 * @author 许天航
 * @ClassName: QuotationController
 * @Description:
 * @date 2017年01月09日
 */

@Controller
@RequestMapping(value = "${adminPath}/customization/contract/")
public class CustomizationContractController extends BaseController {

    @Autowired
    private CustomizationContractService customizationContractService;
    @Autowired
    Configuration freemarkerConfiguration;


    /**
     * 大货订单一览画面
     *
     * @return
     */
    @RequestMapping(value = {""})
    public String customizationContract() {
        return "modules/order/customizationContract";
    }

    /**
     * 拼接一览画面
     *
     * @param values
     * @return
     */
    @RequiresPermissions("customization:contract:view")
    @RequestMapping(value = {"searchList"})
    @ResponseBody
    public String searchList(CustomizationContract customizationContract, HttpServletRequest request, HttpServletResponse response,
                             Model model, String[] values) throws Exception {
        Map <Object, Object> listMap = new HashMap <Object, Object> ();
        // ajax回调时使用的一览list名
        request.setAttribute ("listNames", "customizationContract");
        // 画面取得的检索项目
        String contractNO = customizationContract.getContractNO ();
        // 客户名称
        String venderName = customizationContract.getVenderName ();
        // 开始日期
        String dateStart = customizationContract.getDateStart ();
        // 截止日期
        String dateEnd = customizationContract.getDateEnd ();
        // 拼接字符串供分页使用
        String searchParam = "&contractNO=" + contractNO + "&venderName=" + venderName + "&dateStart=" + dateStart + "&dateEnd=" + dateEnd;
        //放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
        request.setAttribute (Global.SEARCH_PAEAM, searchParam);
        // 查询一览页面数据
        Page <CustomizationContract> page = customizationContractService.findCustomizationContractList (new Page <CustomizationContract> (request, response),
        		customizationContract);
        // 读取文件生成字符串
        String strUri = request.getContextPath () + Global.getAdminPath ();
        String strHtml = "";
        Map <String, Object> map = new HashedMap ();
        map.put ("adminUri", strUri);
        map.put ("contractList", page.getList ());
        map.put ("pageNo", page.getPageNo ());
        map.put ("pageSize", page.getPageSize ());
        strHtml = FreeMarkerTemplateUtils
                .processTemplateIntoString (freemarkerConfiguration.getTemplate ("customizationContractList.ftl"), map);
        listMap.put ("customizationContract", strHtml);
        listMap.put ("page", page.getHtml ());
        // ajax返回结果
        return renderString (response, listMap);
    }

    /**
     * 功能：Java读取txt文件的内容 1：先获得文件句柄 2：获得文件句柄当做是输入一个字节码流，需要对这个输入流进行读取
     * 3：读取到输入流后，需要读取生成字节流
     *
     * @param filePath
     * @return html
     */
    public String readTxtFile(String filePath) {
        String strHtml = "";
        try {
            String encoding = "UTF-8";
            File file = new File (filePath);
            // 判断文件是否存在
            if (file.isFile ()) {
                InputStreamReader read = new InputStreamReader (
                        // 考虑到编码格式
                        new FileInputStream (file), encoding);
                BufferedReader bufferedReader = new BufferedReader (read);
                String lingText = null;
                while ((lingText = bufferedReader.readLine ()) != null) {
                    strHtml += lingText;
                }
                read.close ();
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return strHtml;
    }

    

   

    /**
     * 查看一条大货订单信息
     *
     * @return
     */
    @RequiresPermissions("customization:contract:view")
    @RequestMapping(value = "contractDetail")
    public String contractDetail(CustomizationContract customizationContract, HttpServletRequest request, HttpServletResponse response,
                                    Model model) {
    	customizationContract = customizationContractService.getContractInfo (customizationContract);
        model.addAttribute ("customizationContract", customizationContract);
        return "modules/order/customizationContractShow";
    }

   



}